{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "1D Maxwellian distribution function\n",
    "===================================\n",
    "\n",
    "We import the usual modules, and the hero of this notebook,\n",
    "the Maxwellian 1D distribution:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "import astropy.units as u\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from astropy.constants import k_B, m_e"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {
    "raw_mimetype": "text/restructuredtext"
   },
   "source": [
    "Take a look at the docs to :func:`~plasmapy.formulary.distribution.Maxwellian_1D` for more information on these."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Given we'll be plotting, import astropy's quantity support:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "from astropy.visualization import quantity_support\n",
    "\n",
    "from plasmapy.formulary import Maxwellian_1D\n",
    "\n",
    "quantity_support()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As a first example, let's get the probability density of\n",
    "finding an electron with a speed of 1 m/s if we have a\n",
    "plasma at a temperature of 30 000 K:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "p_dens = Maxwellian_1D(\n",
    "    v=1 * u.m / u.s, T=30000 * u.K, particle=\"e\", v_drift=0 * u.m / u.s\n",
    ")\n",
    "print(p_dens)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note the units! Integrated over speed, this will give us a\n",
    "probability. Let's test that for a bunch of particles:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "T = 3e4 * u.K\n",
    "dv = 10 * u.m / u.s\n",
    "v = np.arange(-5e6, 5e6, 10) * u.m / u.s"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Check that the integral over all speeds is 1\n",
    "(the particle has to be somewhere):\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "tags": [
     "nbsphinx-thumbnail"
    ]
   },
   "outputs": [],
   "source": [
    "for particle in [\"p\", \"e\"]:\n",
    "    pdf = Maxwellian_1D(v, T=T, particle=particle)\n",
    "    integral = (pdf).sum() * dv\n",
    "    print(f\"Integral value for {particle}: {integral}\")\n",
    "    plt.plot(v, pdf, label=particle)\n",
    "plt.legend()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The standard deviation of this distribution should give us back the\n",
    "temperature:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "std = np.sqrt((Maxwellian_1D(v, T=T, particle=\"e\") * v**2 * dv).sum())\n",
    "T_theo = (std**2 / k_B * m_e).to(u.K)\n",
    "\n",
    "print(\"T from standard deviation:\", T_theo)\n",
    "print(\"Initial T:\", T)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
